package chainofresponsibility;

import chainofresponsibility.twoway.FilterChain;
import chainofresponsibility.twoway.Request;
import chainofresponsibility.twoway.Response;

/**
 * 过滤接口
 */


public class TwoWay {
    public static void main(String[] args) {
        System.out.println("===============同一个过滤器过滤 request response=============");

        Request request = new Request();
        Response response = new Response();

        FilterChain filterChain = new FilterChain();

        // 责任链B
        FilterChain filterChainB = new FilterChain();
        filterChainB.add((req,res,fc)->{
            System.out.println("filter1-request-B");

            req.str += "B1";

            fc.doFilter(req,res,fc);

            res.str += "B1";

            System.out.println("filter1-response-B");
            return true;
        }).add((req,res,fc)->{
            System.out.println("filter2-request-B");

            req.str += "B2";

            fc.doFilter(req,res,fc);

            res.str += "B2";

            System.out.println("filter2-response-B");
            return true;
        });

        // 责任链A
        filterChain.add((req,res,fc)->{
            System.out.println("filter1-request-A");

            req.str += "A1";

            fc.doFilter(req,res,fc);

            res.str += "A1";

            System.out.println("filter1-response-A");
            return true;
        }).add((req,res,fc)->{
            System.out.println("filter2-request-A");

            req.str += "A2";

            fc.doFilter(req,res,fc.getNextChain());

            res.str += "A2";

            System.out.println("filter2-response-A");
            return true;
        }).add(filterChainB).doFilter(request, response, filterChain);

        System.out.println(request);
        System.out.println(response);
    }
}
